package com.bds.tjj;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

import com.bds.tjj.snatch.StatsMouthThread;
import com.bds.tjj.task.NewsThread;
import com.bds.tjj.utils.SpringContextUtil;

/**
 * Hello world!
 */
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		ApplicationContext application = SpringApplication.run(App.class);
		SpringContextUtil.setApplicationContext(application);
		ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
		// 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
		scheduled.scheduleAtFixedRate(new NewsThread(), 0, 1, TimeUnit.DAYS);
		scheduled.scheduleAtFixedRate(new StatsMouthThread(), 0, 1, TimeUnit.DAYS);
		// scheduled.scheduleAtFixedRate(new StatsQuarterThread(), 0, 1,
		// TimeUnit.DAYS);

	}

	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {

		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {

				ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/page/error/extra_404.html");
				ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/page/error/extra_404.html");
				ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/page/error/extra_500.html");

				container.addErrorPages(error401Page, error404Page, error500Page);
			}
		};
	}
}
